﻿using GameEngineer.BaseMatrix;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GameEngineer.BaseShape
{
    public class TriangleShape : BaseShape
    {
        //起初的点
        public Vertex.Vertex4 mOriginVertexA { get; set; }
        public Vertex.Vertex4 mOriginVertexB { get; set; }
        public Vertex.Vertex4 mOriginVertexC { get; set; }

        //变化的点
        public Vertex.Vertex4 mCurrentVertexA { get; set; }
        public Vertex.Vertex4 mCurrentVertexB { get; set; }
        public Vertex.Vertex4 mCurrentVertexC { get; set; }

        private TriangleShape() { }

        public TriangleShape(Vertex.Vertex4 vertexA, Vertex.Vertex4 vertexB, Vertex.Vertex4 vertexC)
        {
            this.mOriginVertexA = mCurrentVertexA = new Vertex.Vertex4(vertexA);
            this.mOriginVertexB = mCurrentVertexB = new Vertex.Vertex4(vertexB);
            this.mOriginVertexC = mCurrentVertexC = new Vertex.Vertex4(vertexC);           
        }

        //画出形状（根据点描出线条）
        public override void Draw(Graphics graphics)
        {
            graphics.DrawLines(new Pen(Color.Black, 2f), this.GetAllPointF().ToArray());

            System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
            path.AddLines(this.GetAllPointF().ToArray());

            int dot = (int)(255 * GetLightDot(new Vertex.Vertex4(0, 0, 250, 1)));
            Color color = Color.FromArgb(dot,dot,dot);
            Brush redBrush = new SolidBrush(color);
            
            graphics.FillPath(redBrush, path);
        }

        //矩阵变换
        public override void Transform(Matrix4X4 matrix)
        {
            this.mCurrentVertexA = matrix.Mul(this.mOriginVertexA);
            this.mCurrentVertexB = matrix.Mul(this.mOriginVertexB);
            this.mCurrentVertexC = matrix.Mul(this.mOriginVertexC);
        }

        //获取各个点屏幕点
        public List<PointF> GetAllPointF()
        {
            List<PointF> pointFList = new List<PointF>();

            pointFList.Add(GetPointF(this.mCurrentVertexA));
            pointFList.Add(GetPointF(this.mCurrentVertexB));
            pointFList.Add(GetPointF(this.mCurrentVertexC));
            pointFList.Add(pointFList[0]);            

            return pointFList;
        }

        //获取点的屏幕点
        public PointF GetPointF(Vertex.Vertex4 vertex4)
        {
            PointF pointF = new PointF();
            pointF.X = (float)(vertex4.mVertexX / vertex4.mVertexW);
            pointF.Y = (float)(vertex4.mVertexY / vertex4.mVertexW);

            return pointF;
        }

        //得到三角形的法线
        public override Vertex.Vertex4 GetNormal()
        {
            //左手法则
            Vertex.Vertex4 U = this.mCurrentVertexB - this.mCurrentVertexA;
            Vertex.Vertex4 V = this.mCurrentVertexC - this.mCurrentVertexA;
            this.mNormal = U.Cross(V);

            return this.mNormal;
        }

        //得到灯光角度
        public override float GetLightDot(Vertex.Vertex4 lightingDir)
        {
            float dot = GetNormal().Normalized.Dot(lightingDir.Normalized);
            return Math.Max(0,dot);
        }
    }
}
